package com.tiennt.android.callnotes.models.listadapters;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.tiennt.android.callnotes.R;
import com.tiennt.android.callnotes.models.contentholders.SingleNote;
import com.tiennt.android.callnotes.models.viewholders.ListNotesByContactViewHolder;

public class ListNotesByContactAdapter extends BaseAdapter {
	private Activity mActivity;
	private ArrayList<SingleNote> mListItems;
	private ArrayList<Integer> mSelectedItems = new ArrayList<Integer>();

	private final String TAG = "ListNotesAdapter";
	private final boolean DBG = true;

	public ListNotesByContactAdapter(Activity activity,
			ArrayList<SingleNote> objects) {
		// TODO Auto-generated constructor stub
		mActivity = activity;
		mListItems = objects;
	}

	public void setNewSelection(int position) {
		if (!mSelectedItems.contains(position)) {
			mSelectedItems.add(position);
			notifyDataSetChanged();
		}
	}

	public boolean isPositionChecked(int position) {
		return mSelectedItems.contains(position);
	}

	public ArrayList<Integer> getCurrentCheckedPosition() {
		return mSelectedItems;
	}

	public void removeSelection(int position) {
		if (mSelectedItems.contains(position)) {
			mSelectedItems.remove(mSelectedItems.indexOf(position));
			notifyDataSetChanged();
		}
	}

	public void clearSelection() {
		mSelectedItems.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListItems.size();
	}

	public void setListData(ArrayList<SingleNote> data) {
		mListItems = data;
	}

	@Override
	public SingleNote getItem(int position) {
		// TODO Auto-generated method stub
		return mListItems.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		ListNotesByContactViewHolder viewHolder = null;

		if (v == null) {
			v = LayoutInflater.from(mActivity).inflate(
					R.layout.list_notes_by_contact_row, null);
			viewHolder = new ListNotesByContactViewHolder(v);
			v.setTag(viewHolder);

		} else {
			viewHolder = (ListNotesByContactViewHolder) v.getTag();
		}

		final SingleNote item = mListItems.get(position);

		if (item != null) {
			viewHolder.getTvNoteTitle().setText(item.getNoteTitle());
			viewHolder.getTvNoteContent().setText(item.getNoteContent());
		}
		return v;
	}

	public void removeAt(int index) {
		if (index >= mListItems.size()) {
			return;
		}
		mListItems.remove(index);
		notifyDataSetChanged();
	}

	public void log(String msg) {
		if (DBG) {
			Log.d(TAG, msg);
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
}
